
class Solution {
    public class ListNode {
        public var val: Int
        public var next: ListNode?
        public init(_ val: Int) {
            self.val = val
            self.next = nil
        }
    }
    
    func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
        let resultNode = ListNode(0)
        var currNode = resultNode
        var number1 = l1
        var number2 = l2
        var isUp = false
        while number1 != nil || number2 != nil {
            let result: Int = (number1?.val ?? 0) + (number2?.val ?? 0) + (isUp ? 1 : 0)
            isUp = result >= 10
            currNode.next = ListNode(result % 10)
            currNode = currNode.next!
            number1 = number1?.next
            number2 = number2?.next
        }
        if isUp {
            currNode.next = ListNode(1)
        }
        return resultNode.next
        
    }
}
